package com.zhou.JavaAndroidFirm.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ListView;

import com.zhou.JavaAndroidFirm.R;
import com.zhou.JavaAndroidFirm.bean.CircleItem;
import com.zhou.JavaAndroidFirm.bean.PageBean;
import com.zhou.JavaAndroidFirm.presenter.CircleZonePresenter;

import java.util.List;

/**
 * @author Administrator
 * @date 2018/7/24 0024
 * @des
 */
public class CircleAdapter extends BaseAdapter {
    public static final int ITEM_VIEW_TYPE_DEFAULT = 0;
    public static final int ITEM_VIEW_TYPE_IMAGE = 1;
    public static final int ITEM_VIEW_TYPE_URL = 2;
    private Context mContext;
    private CircleZonePresenter mPresenter;
    PageBean pageBean;
    List<CircleItem> mData;

    public void setPageBean(PageBean pageBean) {
        this.pageBean = pageBean;
    }

    public PageBean getPageBean() {
        return pageBean;
    }


    public CircleAdapter(Context context, CircleZonePresenter mPresenter, List<CircleItem> data) {
        this.mContext = context;
        this.mPresenter = mPresenter;
        this.mData = data;
        pageBean = new PageBean();

    }


//    @Override
//    public CircleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
//        return CircleViewHolder.create(mContext, viewType);
//    }
//
//    @Override
//    protected void convert(CircleViewHolder helper, CircleItem item) {
//        helper.setData(mPresenter, item, helper.getAdapterPosition());
//    }


    public void addAll(List<CircleItem> circleItems) {
        if (null != circleItems && circleItems.size() > 0) {
            mData.addAll(circleItems);
            notifyDataSetChanged();
        }
    }

    public void reset(List<CircleItem> circleItems) {
        mData.clear();
        if (null != circleItems && circleItems.size() > 0) {
            mData.addAll(circleItems);
        }
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return null == mData ? 0 : mData.size();
    }

    @Override
    public CircleItem getItem(int i) {
        return mData.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }


    @Override
    public int getItemViewType(int position) {
        return mData.get(position).getItemType();
    }

    @Override
    public View getView(int pos, View view, ViewGroup viewGroup) {
        CircleViewHolder holder;
        if (null == view) {
            view = View.inflate(mContext, R.layout.item_circle_list, null);
            holder = CircleViewHolder.create(mContext, view, getItemViewType(pos));
            view.setTag(holder);
        } else {
            holder = (CircleViewHolder) view.getTag();
        }
        holder.setData(mPresenter, getItem(pos), pos);
        return view;
    }

    public Integer getItemCount() {
        return null == mData ? 0 : mData.size();
    }

    public List<CircleItem> getData() {
        return mData;
    }


    public void add(int i, CircleItem circleItem) {
        if (null == circleItem) return;
        mData.add(i, circleItem);
        notifyDataSetChanged();
    }

    public void remove(int position) {
        mData.remove(position);
        notifyDataSetChanged();
    }

    /**
     * 更新单条数据
     *
     * @param circlePosition
     */
    public void notifyItemChanged(int circlePosition, ListView listView) {
        int startShownIndex = listView.getFirstVisiblePosition() - listView.getHeaderViewsCount();
        int endShownIndex = listView.getLastVisiblePosition();

        if (circlePosition >= startShownIndex && circlePosition <= endShownIndex) {
            View view = listView.getChildAt(circlePosition - startShownIndex);
            this.getView(circlePosition, view, listView);
        }
    }
}
